#include "be_alloc.h"
#include "be_simple_alloc.h"
#include "cd_mem_pool.h"



CD_NAMESPACE_BEGIN


c_mem_pool::c_mem_pool(s32	escal,	s32	step):
	m_escal(escal),m_step(step),
		m_minsize(escal),m_maxsize(escal*step)
{
	m_cache	=	(be::simple_mem_pool**)be::alloc::allocate(
		sizeof(be::simple_mem_pool*)	*	step);
	memset(m_cache,	0,	sizeof(be::simple_mem_pool*)	*	step);
	for(s32	i	=	0;	i	<	step;	++i){
		m_cache[i]	=	new	be::simple_mem_pool(escal	*	(i+	1));
	}
}
c_mem_pool::~c_mem_pool()
{
	for(int	i	=	0;	i	<	m_step;	++i){
		delete	m_cache[i];
	}
	be::alloc::deallocate(m_cache,	
		sizeof(be::simple_mem_pool*)	*	m_step);
}
void*	c_mem_pool::allocate		(s32	sz)
{
	if(sz	>	m_maxsize){
		return	new	s8[m_maxsize];
	}
	s32		idx	=	(sz	-	1)/m_escal;	
	return	m_cache[idx]->allocate();
}
void	c_mem_pool::deallocate	(void*	pt,	s32	sz)
{
	if(sz	>	m_maxsize){
		delete	[]	(s8*)pt;
	}
	s32		idx	=	(sz	-	1)/m_escal;	
	m_cache[idx]->deallocate(pt);
}

CD_NAMESPACE_END